//
//  AEUIManager.cpp
//  GameJam-Mac
//
//  Created by Clover Sin on 12/26/11.
//  Copyright (c) 2011 Snow Poem Society. All rights reserved.
//

#include "stdafx.h"

#include "AEUIManager.h"

AEUIManager* AEUIManager::s_AEUIManager = NULL;

AEButton* AEUIManager::addButton(Vector2 coor, AEDelegate d)
{
    AEButton* button = new AEButton(_rootControl, _currentTheme);
    button->setControlPosition(coor);
    button->setDelegate(d);
    
    button->prepareForAddingToWorld();
    theWorld.Add(button);
    
    return button;
}

AELabel* AEUIManager::addLabel(Vector2 coor, const String& text)
{
    AELabel* label = new AELabel(_rootControl, _currentTheme);
    label->setControlPosition(coor);
    label->setText(text);
    
    label->prepareForAddingToWorld();
    theWorld.Add(label);
    
    return label;
}

AEUIManager::AEUIManager(): 
    _enabled(true), 
    _controlWhoIsHandling(NULL), 
    _currentTheme(new AETheme()), 
    _rootControl(NULL), 
    _activeControl(NULL), 
    _controlTouchedAFrameBefore(NULL)
{
    _rootControl = new AEControl(NULL, NULL);
}

AEUIManager::~AEUIManager()
{
    
}

void AEUIManager::setTheme(AETheme *theme)
{
    // TODO: Implement theme class first.
}

void AEUIManager::ReceiveMessage(Message *message)
{
    
}

void AEUIManager::MouseMotionEvent(Vec2i co)
{
    Vector2 coor(co.X, co.Y);
    _mousePosition = coor;
    
    // Check if any control occupies this action.
    if (_controlWhoIsHandling)
    {
        _controlWhoIsHandling->_MouseMotion(coor);
        return;
    }
    
    // All the controls are at normal state.
    AEControl* control = _rootControl->getControlAt(coor);
    // Move mouse from one control to another.
    if (control)
    {
        _activeControl = control;
        
        // Touch a new control.
        if (_controlTouchedAFrameBefore != control)
        {
            // Mouse cursor left the old touched one if it exists.
            if (_controlTouchedAFrameBefore)
            {
                _controlTouchedAFrameBefore->_MouseLeave(coor);
                _controlTouchedAFrameBefore = control;
            }
            
            control->MouseEnter(coor);
            
            // Store the new control as prev-touch one.
            _controlTouchedAFrameBefore = control;
        }
        
        control->_MouseHover(coor);
    }
    else    // Now the mouse cursor does not point to any controls. 
    {
        // Is mouse just leave a control ?
        if (_controlTouchedAFrameBefore)
        {
            _controlTouchedAFrameBefore->_MouseLeave(coor);
            _controlTouchedAFrameBefore = NULL;
            _activeControl = NULL;
        }
    }
}

void AEUIManager::MouseDownEvent(Vec2i coor, MouseButtonInput button)
{
    // Check if any control occupies this action.
    if (_controlWhoIsHandling)
    {
        _controlWhoIsHandling->_MouseDown(coor, button);
    }
    
    // All the controls are at normal state.
    AEControl* control = _rootControl->getControlAt(coor);
    if (control)
    {
        _activeControl = control;
        
        // For mouse click event test.
        control->_MouseDown(coor, button);
    }
}

void AEUIManager::MouseUpEvent(Vec2i coor, MouseButtonInput button)
{
    // Check if any control occupies this action.
    if (_controlWhoIsHandling)
    {
        _controlWhoIsHandling->_MouseUp(coor, button);
    }
    
    // All the controls are at normal state.
    AEControl* control = _rootControl->getControlAt(coor);
    if (control)
    {
        _activeControl = control;
        
        // For mouse click event.
        control->_MouseUp(coor, button);
    }
}

void AEUIManager::MouseWheelEvent(int position)
{
    // TODO: Add mouse wheel support controls and modify this.
}

void AEUIManager::Update(float dt)
{
    if (_controlWhoIsHandling)
    {
        _controlWhoIsHandling->_Update(dt);
    }
}
